<!--开启页面选项卡后，显示为选项卡标题-->
<title>订单</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>订单管理</cite></a>
    <a layadmin-event="back">返回列表</a>
    <a><cite>订单详情</cite></a>
  </div>
</div>

<div class="layui-fluid layui-form">
  <script type="text/html" template lay-url="admin/orders/{{layui.router().search.id}}"
          lay-done="layui.data.done(d.data)">
    {{# var order = d.data; }}
    <div class="layui-card">
      <div class="layui-card-header">商品信息</div>
      <div class="layui-card-body">
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">商品名称</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.product_name}}
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">商品价格</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.display_sale_price}} 元
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">是否转售</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.is_resale ? '是': '否'}}
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="layui-card">
      <div class="layui-card-header">订单信息</div>
      <div class="layui-card-body">
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">订单编号</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.order_no}}
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">下单时间</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.created_at}}
            </div>
          </div>
        </div>

        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">购买数量</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.quantity}}
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">订单金额</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.display_order_amount}} 元
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-form-label">购买用户</div>
          <div class="layui-input-inline layui-form-mid layui-word-aux">
            {{# var user = order.user}}
            {{user.nickname}}({{user.mobile}})
          </div>
        </div>

        <div class="layui-form-item">
          {{# var status = order.order_status; }}
          <div class="layui-inline">
            <div class="layui-form-label">订单状态</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{# if (status.value === 'cancel' || status.value === 'auto_cancel') { }}
              <span class="layui-font-gray">{{status.description}}</span>
              {{# } }}
              {{# if (status.value === 'paid') { }}
              <span class="layui-font-green">{{status.description}}</span>
              {{# } }}
              {{# if (status.value === 'not_pay') { }}
              <span class="layui-font-red">{{status.description}}</span>
              {{# } }}
            </div>
          </div>
          <div class="layui-inline">
            {{# var payment = order.payment || {}; }}
            {{# if (status.value == 'not_pay') { }}
            <div class="layui-form-label">过期时间</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.auto_cancel_time}}
            </div>
            {{# } else if (status.value == 'paid') { }}
            <div class="layui-form-label">付款时间</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{payment.pay_time}}
            </div>
            {{# } else if (status.value == 'canceled') { }}
            <div class="layui-form-label">取消时间</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{order.updated_at}}
            </div>
            {{# } }}
          </div>
        </div>
      </div>
    </div>
    {{# if (order.payment) { }}
    {{# let payment = order.payment; }}
    <div class="layui-card">
      <div class="layui-card-header">支付信息</div>
      <div class="layui-card-body">
        <div class="layui-form-item">
          <div class="layui-form-label">支付单号</div>
          <div class="layui-form-mid layui-word-aux">
            {{payment.out_order_no}}
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">支付状态</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{payment.pay_status.description}}
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">收款账号</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{payment.merchant_no}}
            </div>
          </div>
        </div>
        {{# if (payment.pay_status.value === 'finished') { }}
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">支付方式</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{payment.pay_way.description}}
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">支付时间</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{payment.pay_time}}
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">支付金额</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{payment.pay_amount / 100}} 元
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">实收金额</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{(payment.receive_amount || 0) / 100}} 元
            </div>
          </div>
        </div>
        {{# } }}
      </div>
    </div>
    {{# if (payment.pay_status.value === 'finished') { }}
    <div class="layui-card">
      <div class="layui-card-header">分账信息</div>
      <div class="layui-card-body">
        <div class="layui-form-item">
          <div class="layui-form-label">分账状态</div>
          <div class="layui-input-inline layui-form-mid layui-word-aux">
            {{payment.divide_status.description}}
          </div>
        </div>
        {{# if (payment.divide_status.value === 'failed') { }}
        <div class="layui-form-item">
          <div class="layui-form-label">失败原因</div>
          <div class="layui-form-mid layui-word-aux">
            {{payment.divide_fail_reason || ''}}
          </div>
        </div>
        {{# } }}
        {{# if (payment.divide_status.value === 'divided') { }}
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">平台收入</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{payment.platform_receive_amount / 100}} 元
            </div>
          </div>
          <div class="layui-inline">
            <div class="layui-form-label">代理收入</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{(payment.agent_receive_amount || 0) / 100}} 元
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="layui-inline">
            <div class="layui-form-label">用户收入</div>
            <div class="layui-input-inline layui-form-mid layui-word-aux">
              {{payment.user_receive_amount / 100}}
            </div>
          </div>
        </div>
      </div>
      {{# } }}
    </div>
    </div>
    {{# } }}
    {{# } }}
    {{# if (order.order_status.value === 'paid') { }}
    <div class="layui-card">
      <div class="layui-card-header">资产交易</div>
      <div class="layui-card-body">
        <div class="layui-form-item">
          <div class="layui-form-label">交易状态</div>
          <div class="layui-input-inline layui-form-mid layui-word-aux">
            {{order.trans_status ? order.trans_status.description : ''}}
          </div>
        </div>
        {{# if (order.trans_status && order.trans_status.value === 'failed') { }}
        <div class="layui-form-item">
          <div class="layui-form-label">失败原因</div>
          <div class="layui-form-mid layui-word-aux">
            {{order.trans_fail_reason}}
            <input type="button" class="layui-btn layui-btn-xs" value="重试" id="btn_re_transfer">
          </div>
        </div>
        {{# } }}
        <div class="layui-form-item">
          <div id="table_assets"></div>
        </div>
      </div>
    </div>
    {{# } }}
  </script>
</div>
<script type="text/html" id="tpl_asset_status">
  {{d.asset_status.description}}
</script>

<script>
  layui.data.done = function (obj) {
    layui.use([], function () {
      let table = layui.table
        , $ = layui.$
        , admin = layui.admin
      table.render({
        elem: '#table_assets'
        , data: obj.assets
        , cols: [[
          {title: '存证ID', field: 'token_id', minWidth: 300, align: "center"}
          , {title: '资产交易凭证', field: 'trans_hash', minWidth: 100, align: "center"}
          , {title: '资产状态', templet: '#tpl_asset_status', width: 100, align: "center"}
        ]]
      })

      $('#btn_re_transfer').click(function () {
        layer.confirm('确定重新发起转账?', function (index) {
          admin.post('admin/orders/' + obj.id + '/re_transfer', {}, function (res) {
            admin.events.refresh()
          })
        });
      })
    })
  }
</script>
